package com.example.myapplication.module1.activity

import android.text.Html
import com.example.myapplication.databinding.ActivityBroadcastDetailBinding
import com.example.myapplication.entity.BroadcastBean
import com.example.myapplication.module1.presenter.BroadcastDetailPresenter
import com.example.myapplication.soaprequest.SoapEnvelop
import com.example.myapplication.soaprequest.request.BroadcastDetailRequest
import com.example.myapplication.soaprequest.request.SoapRequestBody
import com.example.myapplication.widget.CustomerToolbar
import com.example.mylibrary.base.BaseMvpActivity
import com.example.mylibrary.utils.GsonUtil
import com.google.gson.reflect.TypeToken
import com.lxj.xpopup.impl.LoadingPopupView

/**
 * 公告详情页面
 */
class BroadcastDetailActivity :
    BaseMvpActivity<BroadcastDetailActivity, BroadcastDetailPresenter, ActivityBroadcastDetailBinding>() {
    private var broadcast: BroadcastBean? = null
    private var title: String? = ""
    lateinit var loadPopup: LoadingPopupView
    private val broadcastDetailRequest = BroadcastDetailRequest()
    private val soapRequestBody = SoapRequestBody()
    private val soapEnvelop = SoapEnvelop()
    override fun initData() {
        broadcast = bundleExtra?.getParcelable<BroadcastBean>("broadcast")
        title = bundleExtra?.getString("title")

        viewBinding.clToolbar.setTitle(title)
        viewBinding.tvContentTitle.text = broadcast?.title
        viewBinding.tvTime.text = broadcast?.opdate

        getBroadcastDetail()
    }

    private fun getBroadcastDetail() {
        broadcastDetailRequest.id = broadcast?.id ?: ""
        soapRequestBody.value = broadcastDetailRequest
        soapEnvelop.body = soapRequestBody
        mPresenter?.getBroadcast(soapEnvelop)
    }

    override fun initView() {
        loadPopup = builder.asLoading("加载中...")
        viewBinding.clToolbar.clickListener = object : CustomerToolbar.ClickListener {
            override fun onBackListener() {
                finish()
            }

            override fun onRightListener() {
                setResult(RESULT_OK)
                finish()
            }
        }
    }

    override fun getBinding(): ActivityBroadcastDetailBinding {
        return ActivityBroadcastDetailBinding.inflate(layoutInflater)
    }

    override fun getPresenter(): BroadcastDetailPresenter {
        return BroadcastDetailPresenter()
    }

    fun setBroadcastDetail(result: String) {
        if (result.isNotEmpty()) {
            val broadcastBeans = GsonUtil.fromJson<MutableList<BroadcastBean>>(
                result,
                object : TypeToken<MutableList<BroadcastBean>>() {}.type
            )
            if (!broadcastBeans.isNullOrEmpty() && broadcastBeans.size > 0) {
                val broadcastBean = broadcastBeans[0]
                viewBinding.tvDetail.text = Html.fromHtml(broadcastBean.memo)
            }
        }
    }

}
